<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['params']=array();
$return['status']=$this->order_status;
$return['buytype']=$this->buy_types;
$return['sendtype']=$this->send_types;
$return['send_status']=$this->send_status;
$return['paytype']=get_pay_types();
$return['paytype']['peerpay']='找人代付';
$return['ispay']=array('0'=>'未支付','1'=>'已支付');
$tabs=array(
			array('op'=>'list','title'=>'订单管理'),
			array('op'=>'goodscount','title'=>'配送统计'),
			array('op'=>'membergoods','title'=>'客户配送情况'),
			array('op'=>'updele','title'=>'刷新已删除的单'),
);
if($_RQ['op']=='list'){
	$return['setting']=$this->setting;
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'deleted'=>0,'plugin_name <>'=>'jy_mfuwu');
	$return['params']=array('deleted'=>0);
    $orderby='createtime desc';
	$total='';
    if(!empty($_RQ['mid'])){
        $params['mid']=trim($_RQ['mid']);
        $return['params']['mid']=$_RQ['mid'];
    }
	if(!empty($_RQ['ordersn'])){
		$params['ordersn']=trim($_RQ['ordersn']);
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	if(isset($_RQ['status']) && $_RQ['status'] !='100'){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}else{
		$return['params']['status']=100;
	}
	if(isset($_RQ['sendtype'])){
		$params['sendtype']=intval($_RQ['sendtype']);
		$return['params']['sendtype']=$_RQ['sendtype'];
	}
	if(isset($_RQ['buytype'])){
		$params['buytype']=intval($_RQ['buytype']);
		$return['params']['buytype']=$_RQ['buytype'];
	}
	if($_RQ['postdata']){
		$postdata=get_postdata();
		
	}
	if(trim($postdata['keyword'])){
		$params['ordersn']=trim($_RQ['ordersn']);
		$return['params']['ordersn']=trim($postdata['keyword']);
		
	}
	$return['list']=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$ordergoods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$v['id'],'merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']),array('id','goodsid','goodsnum','orderid','optionid','price','total','total_price','goodssn','productsn','fee_desc'));
		$v['goods']=array();
		//用户地址
		$v['address']=json_decode($v['address'],true);
        if($v['address'] && ($v['address']['mobile'] || $v['address']['name'])){
			$address=$v['address'];
		}else{
			$address=pdo_get('core_address',array('uniacid'=>$_SESSION['uniacid'],'id'=>$v['addressid']));
		}
		if(!is_array($address['pcd'])){
            $address['pcd']=json_decode($address['pcd'],true);
        }
		$v['order_out']=$address;
        $v['pcdinfo']=$address['pcd']['city'].' '.$address['pcd']['district'].' '.$address['pcd']['street'];
		$v['goodsnum']=0;
		foreach($ordergoods as &$gv){
			$goodsinfo = pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']),array('id','image','title','merchid'));
			$goodsinfo['optionid']=$gv['optionid'];
			$goodsinfo['total']=$gv['total'];
			$goodsinfo['price']=$gv['price'];
			$goodsinfo['image']=tomedia($goodsinfo['image']);
			$goodsinfo['total_price']=$gv['total_price'];
			if(!$gv['fee_desc']){
					model('jy_weishop/order')->setfee_desc($gv['orderid']);
					$goodsinfo['total_price']=pdo_getcolumn('jy_weishop_order_goods',array('id'=>$gv['id']),'total_price');
			}
			$v['merch_money'] +=$goodsinfo['total_price'];
			//商品规格
			if($gv['optionid']){
				$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$gv['optionid']),'specs');
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$goodsinfo['selespename']=implode("，",$specs);
			}
			$goodsinfo['totalprice']=$goodsinfo['price'];
			$v['goods'][]=$goodsinfo;
			if($v['goodsnum']<$gv['goodsnum']){
				$v['goodsnum']=$gv['goodsnum'];
			}
		}
		$v['merch_money']=price($v['merch_money']);
		$v['commission_money']=price($v['commission_money']);
		$v['sendtypetitle']=$return['sendtype'][$v['sendtype']];
		if($v['send_status'] && ($v['status']==1 || $v['is_dingpei'])){
			$v['statustitle']=$this->send_status[$v['send_status']];
		}else{
			$v['statustitle']=$this->order_status[$v['status']];
		}
		$v['buytypetitle']=$this->buy_types[$v['buytype']];
		$v['member']=cfc('core')->member_show($v['mid']);
		if(!$v['is_dingpei']){
			$v['goodsnum']=1;
		}
		$v['issendnum']=pdo_count('jy_peimilk_sendlog',array('status'=>2,'order_id'=>$v['id']));
		$v['lastsendnum']=$v['goodsnum']-$v['issendnum'];
		$v['peisong_fee']=$this->model('model')->get_peifee($v);
		if($v['peisong_id']){
			$v['peimember']=pdo_get('jy_peimilk_member',array('mid'=>$v['peisong_id'],'uniacid'=>$_SESSION['uniacid']),array('title','mobile','level_id'));
			$v['peimember']['level']=pdo_getcolumn('jy_peimilk_level',array('id'=>$v['peimember']['level_id'],'uniacid'=>$_SESSION['uniacid']),'title');
			$v['peimember']['level']=$v['peimember']['level']?:'普通等级';
		}
		if($_SESSION['os']!='windows'){
			$selfinfo=pdo_get('jy_weishop_order_self',array('orderid'=>$v['id']));
			$selfinfo['selftime']=$selfinfo['selftime']?date('Y-m-d H:i:s',$selfinfo['selftime']):'尽快配送';
			$selfinfo['selftime']=$selfinfo['out_time']?:$selfinfo['selftime'];
			$selfinfo['number2']=substr($selfinfo['number'],-4);
			$v['selfinfo']=$selfinfo;
			$v['createtime']=date('Y-m-d H:i:s', $v['createtime']);
			$v['paytime']=$v['paytime']?date('Y-m-d H:i:s', $v['paytime']):'';
			$peiset=$this->pdo_get('peiset',array('uniacid'=>$_SESSION['uniacid'],'order_id'=>$v['id']));
			$days=str_array($peiset['days']);
			$v['times']=str_array($peiset['times']);
			$daytime= mktime(0,0,0,date('m'),date('d'),date('Y'));
			$v['nowlog']=pdo_get('jy_peimilk_sendlog',array('status'=>0,'daytime >'=>$daytime,'order_id'=>$v['id'],'uniacid'=>$_SESSION['uniacid']),'','daytime asc');
			$v['nowlog']['statustitle']=$this->send_status[$v['nowlog']['status']];
			$v['nowlog']['statustitle']=$v['nowlog']['statustitle']?:'未发起';
			$v['peiday']=$v['nowlog']['day'];
			if($v['peiday']){
				$v['tips']='最近配送时间：'.$v['peiday'];
			}elseif($v['status']==2){
				$v['tips']='客户，没有预约，未来配送的时间！';
			}
		}
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','openvoice'=>true,'openvoice_action'=>'jy_weishop/order.list/getnew/merchid='.$merchid,'edit'=>false,'list'=>array(
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'price','title'=>'金额','type'=>'string'),
			array('name'=>'peisong_id','title'=>'配送员','type'=>'select','options'=>'peisongs','option_value'=>'mid'),
			array('name'=>'is_dingpei','title'=>'配送方式','type'=>'radio','options'=>'is_dingpei'),
			array('name'=>'goodsnum','title'=>'商品数量','type'=>'string'),
            array('name'=>'pcdinfo','title'=>'配送区域','type'=>'string'),
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			array('name'=>'ispay','title'=>'是否支付','type'=>'radio','options'=>'ispay'),
			array('name'=>'paytype','title'=>'支付方式','type'=>'radio','options'=>'paytype'),			
		),
		'action'=>array(	
			array('type'=>'href','action'=>'/merch.order_detail/','title'=>'订单详情'),
			array('op'=>'fa','title'=>'发起配送','type'=>'modal','show'=>array('status'=>1,'sendtype'=>1,'send_status'=>array(0,11))),
			array('op'=>'zhuan','title'=>'转配送员','type'=>'modal','show'=>array('status'=>array(1,2,3),'send_status'=>array(1,3,11,12,13),'sendtype'=>1)),
			array('op'=>'fa','title'=>'自提_Ship','type'=>'modal','show'=>array('status'=>1,'sendtype'=>2,'send_status'=>array(0,11))),
			array('op'=>'fa','title'=>'虚拟发货_Ship','type'=>'modal','show'=>array('status'=>1,'sendtype'=>3,'send_status'=>array(0,11))),
			array('op'=>'print','title'=>'打印','type'=>'post'),
			array('type'=>'post','op'=>'shou','title'=>'确认完成','show'=>array('status'=>2,'is_dingpei'=>0)),
			array('type'=>'post','op'=>'stopsend','title'=>'关闭订单/停止配送','show'=>array('status'=>array(1,2,3,5,6),'is_dingpei'=>1)),
		)),
		'search'=>array(
			array('name'=>'ordersn','width'=>10,'title'=>'订单号','type'=>'string'),
			array('name'=>'status','width'=>10,'title'=>'订单状态','type'=>'radio','options'=>'status'),
			array('name'=>'buytype','width'=>10,'title'=>'订单类型','type'=>'radio','options'=>'buytype'),
			array('name'=>'is_dingpei','width'=>10,'title'=>'配送方式','type'=>'radio','options'=>'is_dingpei'),
			array('name'=>'sendtype','width'=>10,'title'=>'取货方式','type'=>'radio','options'=>'sendtype'),
            array('type'=>'choose','width'=>13,'title'=>'所属会员','name'=>'mid','action'=>'jy_merch/get_member/'),
		)
	);
	$htmls['list']['list'][]=array('name'=>'send_status','title'=>'配送状态','type'=>'radio','options'=>'send_status');
	if($_SESSION['os']=='mobile'){
		//多商户用户，选择平台区域配送员
		$areas=pdo_getall('jy_peimilk_area',array('uniacid'=>$_SESSION['uniacid']));
		foreach($areas as $av){
			$users=pdo_getall('jy_peimilk_member',array('area_id'=>$av['id'],'status'=>1),array('mid','title'));
			foreach($users as $uv){
				$peisongs[]=$uv;
			}
		}
		$return['peisongs']=$peisongs;
	}else{
		$return['peisongs']=$this->getall('member',array('uniacid'=>$_SESSION['uniacid']),array('mid','title'),'mid');
		$return['htmls']=$htmls;
	}
	$return['is_dingpei']=array('一次性配送','定期配送');
	exi($return);
}
if($_RQ['op']=='goodscount'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'status'=>array(0,1,2));
	$sendlog=pdo_getall('jy_peimilk_sendlog',$params);
	$list=array();
	foreach($sendlog as $goodskey=>&$item){
		$goods=json_decode($item['goods'],true);
		foreach($goods as $goodskey=>&$g){
			if($list[$goodskey]){
				$list[$goodskey]['allnum'] +=$g['num'];
				if($item['status']==2){
					$list[$goodskey]['issendnum'] +=$g['num'];
				}
			}else{
				$list[$goodskey]=$g;
				$list[$goodskey]['allnum']=$g['num'];
				if($item['status']==2){
					$list[$goodskey]['issendnum']=$g['num'];
				}
			}
		}
	}
	$list=array_values($list);
	$return['list']=$list;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：(<red>这里显示会员产生了配送记录的数据，如果有些会员，只是下单了，但是还没有产生配送记录，这里不统计！</red>)一次性商品，按数量1计算；定期商品，按套餐数量计算(<red>不重复，配送统计是不分客户的，只分商品；而客户配送情况，分客户，也分商品类型</red>)！','edit'=>false,'total'=>$total,'psize'=>$psize,'del_one'=>false,'delall'=>false,'list'=>array(
				array('name'=>'title','title'=>'商品名称','type'=>'string'),
				array('name'=>'selespename','title'=>'商品规格','type'=>'string'),
				array('name'=>'leititle','title'=>'类型','type'=>'string'),
				array('name'=>'allnum','title'=>'总配送','type'=>'string'),
				array('name'=>'issendnum','title'=>'已配送','type'=>'string'),
				array('name'=>'unit','title'=>'单位','type'=>'string'),		
		)),
	);
	$return['htmls']=$htmls;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='membergoods'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'status'=>array(0,1,2));
	$sendlog=pdo_getall('jy_peimilk_sendlog',$params);
	$list=array();
	foreach($sendlog as $goodskey=>&$item){
		$goods=json_decode($item['goods'],true);
		foreach($goods as $goodskey=>&$g){
			$goodskey2=$item['buymid'].'_'.$goodskey;
			if($list[$goodskey2]){
				$list[$goodskey2]['allnum'] +=$g['num'];
				if($item['status']==2){
					$list[$goodskey2]['issendnum'] +=$g['num'];
				}
			}else{
				$g['mid']=$item['buymid'];
				$g['member']=cfc('core')->member_show($g['mid']);
				$list[$goodskey2]=$g;
				$list[$goodskey2]['allnum']=$g['num'];
				if($item['status']==2){
					$list[$goodskey2]['issendnum']=$g['num'];
				}
			}
		}
	}
	$list=array_values($list);
	paixu($list,'mid','desc');
	$return['list']=$list;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','tip'=>'[温馨提示]：一次性商品，按数量1计算；定期商品，按套餐数量计算！','edit'=>false,'total'=>$total,'psize'=>$psize,'del_one'=>false,'delall'=>false,'list'=>array(
				array('name'=>'mid','title'=>'MID','type'=>'string'),
				array('name'=>'member','title'=>'会员信息','type'=>'member'),
				array('name'=>'title','title'=>'商品名称','type'=>'string'),
				array('name'=>'selespename','title'=>'商品规格','type'=>'string'),
				array('name'=>'leititle','title'=>'类型','type'=>'string'),
				array('name'=>'allnum','title'=>'总配送','type'=>'string'),
				array('name'=>'issendnum','title'=>'已配送','type'=>'string'),
				array('name'=>'unit','title'=>'单位','type'=>'string'),		
		)),
	);
	$return['htmls']=$htmls;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	exi($return);
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'plugin_name <>'=>'jy_mfuwu');
	$params['deleted']=intval($_RQ['deleted']);
	if(!empty($_RQ['ordersn'])){
		$params['ordersn']=trim($_RQ['ordersn']);
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	if(isset($_RQ['status']) && $_RQ['status'] !='100'){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}else{
		$return['params']['status']=100;
	}
	if(isset($_RQ['sendtype'])){
		$params['sendtype']=intval($_RQ['sendtype']);
		$return['params']['sendtype']=$_RQ['sendtype'];
	}
	if(isset($_RQ['buytype'])){
		$params['buytype']=intval($_RQ['buytype']);
		$return['params']['buytype']=$_RQ['buytype'];
	}
	$order=pdo_getall('jy_weishop_order',$params);
	$status=array('1'=>'待发货','2'=>'待收货','3'=>'已完成','4'=>'已关闭');
	$paytype=array('wechat'=>'微信支付','credit'=>'余额支付');
	$ispay=array('0'=>'未支付','1'=>'已支付');
	foreach($order as &$v){
		$v['nickname']=pdo_getcolumn('core_members',array('id'=>$v['mid']),'nickname');		
		$address=$v['address']?json_decode($v['address'],true):$v['addressid'];
		if($address){
			$v['address']=model('core/member')->get_address_str($address);
		}
		$v['mobile']=$address['mobile'];
		$v['realname']=$address['realname']?:$address['name'];
		$v['status']=$status[$v['status']];
		$v['paytype']=$paytype[$v['paytype']];
		$v['ispay']=$ispay[$v['ispay']];
		if(!$v['nickname']){
			$v['nickname']='匿名用户';
		}
		$v['createtime']=date('Y-m-d H:i:s',$v['createtime']);
		$v['storetitle']=pdo_getcolumn('jy_weishop_store',array('id'=>$v['storeid']),'title');
		$v['storetitle']=$v['storetitle']?:'无';
		$goods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$v['id']),array('goodsid','optionid'));
		foreach($goods as $k=>$gv){
			$goodstitle=pdo_getcolumn('jy_weishop_goods',array('id'=>$gv['goodsid']),'title');
			//商品规格
			if($gv['optionid']){
				$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$gv['optionid']),'specs');
				$specs=explode("_",$specs);
				foreach($specs as &$spec){
					$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
				}
				$selespename=implode("，",$specs);
			}else{
				$selespename='无规格';
			}
			$num=$k+1;
			$v['goodstitle'] .='【商品'.$num.'】['.$selespename.']'.$goodstitle;
		}
	}
	$header=array(
		'ordersn'=>'订单号',
		'nickname'=>'买家昵称',
		'price'=>'金额',
		'status'=>'订单状态',
		'ispay'=>'金额',
		'paytype'=>'订单状态',
		'realname'=>'收货人姓名',
		'address'=>'收货地址',
		'mobile'=>'收货人电话',
	);
	cfc('tools')->export2excel($header,$order,'订单');
}
if($_RQ['op']=='fa'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		if($post['sendtype']==1){
			if($post['express_type']==0){
				if(!$post['express'] || !$post['expresssn']){
					exi('请填写快递信息','error');
				}
				$data=array('express'=>$post['express'],'expresssn'=>$post['expresssn']);
				model('jy_weishop/order')->order_send($id,$data);
			}elseif($post['express_type']==1){//平台配送
				if(!$post['peisong_id'] && $post['need_setpeisong']){
					exi('请选择配送员！','error');
				}
				if($post['peisong_id']){
					$params['order_id']=$id;
					$params['peisong_id']=$post['peisong_id'];
					model('jy_peimilk/order')->order_sendget($params);
				}else{
					$data['send_status']=12;
					cfc('jinyun_socket',$_SESSION['uniacid'])->send2room('刷新配送订单！','peisong_order');
					model('jy_weishop/order')->order_send($id,$data);
				}			
				exi('订单配送处理成功！','','/merch.order/list/status=1');
			}elseif($post['express_type']==2){//商家自己配送
				model('jy_weishop/order')->order_send($id);
				exi('订单配送处理成功！','','/merch.order/list/status=1');
			}elseif($post['express_type']==3){//达达配送
				$res=model('jy_weishop/order/dada_api')->dada_send($post);
				if($res['state']==1){
					$data=array('opensend_type'=>1);
					model('jy_weishop/order')->order_send($id,$data);
				}else{
					exi($res['msg'],'error');
				}
			}			
		}elseif($post['sendtype']==3){
			model('jy_weishop/order')->order_send_virtual($id,$post['content']);
		}elseif($post['sendtype']==2){
			model('jy_merch/verify')->self_verify($post['verifycode']);
		}
		exi('已发货！','','/merch.order/list/status=1');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_weishop_order',array('id'=>$id),array('id','is_peisong','sendtype','addressid','ordersn','remark','price','isdish'));	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'订单发货',
	);
	if($item['sendtype']==1){
		$citypeisong=plugin('jy_weishop')->plugin_setting('citypeisong');
		$item['citypeisong_isopen']=$citypeisong['isopen'];
		$item['citypeisong_express_type']=$citypeisong['express_type']?:0;
		if($citypeisong['isopen']==1 || $item['isdish']){
			$item['express_type']=$citypeisong['express_type']?:1;
			if(!$item['isdish'] && $item['is_peisong']==2){
				$item['express_type']=0;
			}
			if($item['express_type']==1){
				$tip='商品订单已打包好,由平台配送员配送！';
			}elseif($item['express_type']==2){
				$tip='商品订单已打包好,商家自己配送,无需配送员！';
			}elseif($item['express_type']==3){
				$tip='商品订单已打包好,发送给达达配送！(<red>【慎重操作】：确定要呼叫《达达配送员》过来拿货吗?</red>)';
			}elseif(!$item['express_type']){
				$tip='商品订单已打包好,快递发货！';
			}
		}else{
			$item['express_type']=0;
			$tip='商品订单已打包好,快递发货！';
		}
		$htmls['edit']=array(
			array('type'=>'tip','title'=>$tip),
			array('type'=>'select','title'=>'快递公司','name'=>'express','options'=>'express','option_title'=>'name','show'=>array('express_type'=>0)),
			array('type'=>'string','title'=>'快递单号','name'=>'expresssn','show'=>array('express_type'=>0)),
			array('type'=>'switch','name'=>'need_setpeisong','title'=>'需要指定配送员？','show'=>array('express_type'=>1),'help'=>'不指定，则由配送员自己抢单，或系统指派！'),
			array('type'=>'select','title'=>'指定配送员','name'=>'peisong_id','options'=>'peisongs','option_value'=>'mid','show'=>array('need_setpeisong'=>1),'help'=>'（<red>配送员必须在线才能被选择</red>）只能指定该商户下面的配送员，或本城市的平台配送员！'),
		);
	}else{
		$goodsid=pdo_getcolumn('jy_weishop_order_goods',array('orderid'=>$id),'goodsid');
		$goods=pdo_get('jy_weishop_goods',array('id'=>$goodsid),array('title','image'));
		$item['verifycode']=pdo_getcolumn('jy_weishop_order_self',array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id']),'number');

		$item=array_merge($goods,$item);
		$htmls['edit']=array(
			array('type'=>'string','title'=>'商品名称','name'=>'title','read'=>true),
			array('type'=>'image','title'=>'商品图片','name'=>'image','read'=>true),
			array('type'=>'string','title'=>'自提核销码','name'=>'verifycode','show'=>array('sendtype'=>2)),
			array('type'=>'text','title'=>'虚拟物品发货内容','name'=>'content','show'=>array('sendtype'=>3),'help'=>'虚拟物品发货，填写发货内容！'),
			
		);
	}
	$return['express']=pdo_getall('jy_weishop_express');
	$peisongs=array();
	//多商户用户，选择平台区域配送员
	$areas=pdo_getall('jy_peimilk_area',array('uniacid'=>$_SESSION['uniacid']));
	foreach($areas as $v){
		$users=pdo_getall('jy_peimilk_member',array('status'=>1,'uniacid'=>$_SESSION['uniacid']),array('mid','title'));
		foreach($users as $uv){
			$uv['title'].='('.$uv['mid'].')';
			$peisongs[]=$uv;
		}
	}
	$return['peisongs']=$peisongs;
	//if($_SESSION['os']=='mobile'){
	//	$return['htmls']=$this->template_url('merch/order_index');
	//}else{
		$return['htmls']=$htmls;
	//}
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='zhuan'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$id=intval($post['id']);
		if(!$id){
			exi('参数错误！','error');
		}
		if($post['sendtype']==1){
			$this->model('order')->up_sendlog($id,'',3,$post['peisong_id']);
			if($post['peisong_id']){
				pdo_update('jy_weishop_order',array('peisong_id'=>$post['peisong_id'],'send_status'=>13),array('id'=>$id));
				$this->model('model')->up_orderuser($post['mid'],$post['merchid'],$post['peisong_id']);
			}else{
				pdo_update('jy_weishop_order',array('peisong_id'=>0,'status'=>1,'send_status'=>12),array('id'=>$id));
				$this->model('model')->up_orderuser($post['mid'],$post['merchid'],'no');
			}
			exi('已转单！','','/merch.order/');
		}
		exi('操作失败！','','/merch.order/');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_weishop_order',array('id'=>$id),array('id','peisong_id','mid','merchid','is_peisong','sendtype','addressid','ordersn','remark','price','isdish'));	
	$htmls=array(
		'type'=>'edit',
		'desc'=>'订单转单',
	);
	if($item['sendtype']==1){
		$tip='当前已经有配送员接单了，确定要转单？';
		$htmls['edit']=array(
			array('type'=>'tip','title'=>$tip),
			array('type'=>'switch','name'=>'need_setpeisong','title'=>'需要指定配送员？','help'=>'不指定，则由配送员自己抢单，或系统指派！'),
			array('type'=>'select','title'=>'指定配送员','name'=>'peisong_id','options'=>'peisongs','option_value'=>'mid','show'=>array('need_setpeisong'=>1),'help'=>'（<red>配送员必须在线才能被选择</red>）只能指定该商户下面的配送员，或本城市的平台配送员！'),
		);
	}

	$peisongs=array();
	//多商户用户，选择平台区域配送员
	$areas=pdo_getall('jy_peimilk_area',array('uniacid'=>$_SESSION['uniacid']));
	foreach($areas as $v){
		$users=pdo_getall('jy_peimilk_member',array('status'=>1,'uniacid'=>$_SESSION['uniacid']),array('mid','title'));
		foreach($users as $uv){
			$uv['title'].='('.$uv['mid'].')';
			$peisongs[]=$uv;
		}
	}
	$return['peisongs']=$peisongs;
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
//转单
if($_RQ['op']=='change'){
	$post=get_postdata();
	$order_id=intval($post['id']);
	if(!$order_id){
		exi('订单不存在，请重新选择订单','error');
	}
	$this->model('order')->up_sendlog($order_id,'',3);
	if($post['peisong_id']){
		pdo_update('jy_weishop_order',array('peisong_id'=>$post['peisong_id'],'send_status'=>13),array('id'=>$order_id));
	}else{
		pdo_update('jy_weishop_order',array('peisong_id'=>0,'status'=>1,'send_status'=>12),array('id'=>$order_id));
	}
	exi('转单成功！');
}
if($_RQ['op']=='ispay'){
	$id=intval($_RQ['id']);
	pdo_update('jy_weishop_order',array('ispay'=>1,'status'=>1,'pay_id'=>0),array('id'=>$id));
	pdo_update('jy_weishop_order_goods',array('clearstatus'=>3),array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$id,'clearstatus'=>0));
	exi('已确认付款','');
}

if($_RQ['op']=='shou'){
	$id=intval($_RQ['id']);
	$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	if($order['is_dingpei']){
		exi('定期配送订单不能该操作！','error');
	}
	model('jy_weishop/order')->order_receive($id);
	exi('已收货','','/merch.order/list/');
}
if($_RQ['op']=='daifa'){
	$id=intval($_RQ['id']);
	pdo_update('jy_weishop_order',array('is_daifa'=>1),array('id'=>$id));
	exi('申请平台代发成功！','','/merch.order/list/');
}
if($_RQ['op']=='deleted'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'deleted'=>1);
	$return['params']=array('deleted'=>1);
    $orderby='createtime desc';
	$total='';
	$return['list']=pdo_getslice('jy_weishop_order',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($return['list'] as &$v){
		$v['member']=cfc('core')->member_show($v['mid']);
	}
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'订单管理'),
		),
		'list'=>array('data'=>'list','edit'=>false,'del_op'=>'ddel','list'=>array(
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'price','title'=>'金额','type'=>'string'),
			array('name'=>'status','title'=>'订单状态','type'=>'radio','options'=>'status'),	
			array('name'=>'createtime','title'=>'下单时间','type'=>'time'),
			array('name'=>'ispay','title'=>'是否支付','type'=>'radio','options'=>'ispay'),
			array('name'=>'paytype','title'=>'支付方式','type'=>'radio','options'=>'paytype'),				
		),
		'action'=>array(	
			array('type'=>'href','action'=>'merch.order_detail/','title'=>'订单详情'),
			
		),),
		
		'search'=>array(
			array('name'=>'order','width'=>15,'title'=>'订单号','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='print'){
	$order_id=intval($_RQ['id']);
	$res=model('jy_weishop/printmsg')->storeprint('',$order_id);
	if($res){
		exi('打印成功');
	}else{
		exi('打印失败,该订单所属门店没有添加打印任务！','error');
	}
}
if($_RQ['op']=='upprice'){
	$id=intval($_RQ['id']);
	$post=get_postdata();	
	if($post['price']){
		if(!is_numeric($post['price'])){
			exi('价格必须是数字！','error');
		}
		if(!method_exists(cfc('pay'),'change_pay_fee')){
			exi('修改价格失败！请联系管理员，更新框架到最新','error');
		}
		$price=floatval($post['price']);
		if ($price < 0) 
		{
			$price = 0;
		}
		model('jy_weishop/order')->upprice($post['id'],$price);
		exi('改价成功！','','jy_merch/merch.order/');	
	}
	$item=pdo_get('jy_weishop_order',array('id'=>$id),array('id','price'));
	$htmls=array(
		'type'=>'edit',
		'desc'=>'修改价格',
	);
	$htmls['edit']=array(
		array('type'=>'string','title'=>'价格','name'=>'price','help'=>'修改价格！'),	
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='stopsend'){
	$id=intval($_RQ['id']);
	pdo_update('jy_weishop_order',array('status'=>-1),array('id'=>$id));
	pdo_update('jy_weishop_order_goods',array('status'=>-1),array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$id));
	$this->model('order')->upstoporder($id);
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_update('jy_weishop_order',array('deleted'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		pdo_update('jy_weishop_order_goods',array('status'=>-1),array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$id));
		$this->model('order')->upstoporder($id);
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_update('jy_weishop_order',array('deleted'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
				pdo_update('jy_weishop_order_goods',array('status'=>-1),array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$id));
				$this->model('order')->upstoporder($id);
			}
		}
	}
	exi('删除成功！','','/merch.order/list/');
}
if($_RQ['op']=='ddel'){

	$id=intval($_RQ['id']);
	if($id){
		$this->model('order')->order_delete($id);	
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$this->model('order')->order_delete($id);
			}
		}
	}
	exi('删除成功！','','/merch.order/deleted');
}
if($_RQ['op']=='updele'){
	if($_RQ['postdata']){
		$orders=pdo_getall('jy_weishop_order',array('deleted'=>1,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid));
		foreach($orders as $item){
			pdo_update('jy_weishop_order_goods',array('status'=>-1),array('uniacid'=>$_SESSION['uniacid'],'orderid'=>$item['id']));
			$this->model('order')->upstoporder($item['id']);
		}
		exi('操作成功！','','/merch.order/');
	}
	$item=array();
	$htmls=array(
		'type'=>'edit',
		'desc'=>'刷新异常单（已删除的单）',
	);
	$tip='之前预留的删除订单，但是部分数据未删除的问题';
	$htmls['edit']=array(
		array('type'=>'tip','title'=>$tip),
	);
	$return['htmls']=$htmls;
	$return['item']=$item;
	exi($return);
}
?>